<?php
class STE_Album extends STE_Media
{	
	public function getAlbumCountByAlbumType($albumType)
	{
		$r = $this->_fetchStatement("CALL sp_admin_get_album_count_by_album_type(?)", array($albumType));
		return empty($r) ? null : $r[0]; 
	}

	public function getAlbumList($albumType, $from, $numberOfAlbums)
	{
		return $this->_fetchStatement("CALL sp_admin_get_album_list(?, ?, ?)", array($albumType, $from, $numberOfAlbums));
	}
	
	private function _getPhotoPathList($albumID)
	{
		return $this->_fetchStatement("CALL sp_admin_get_photo_path_list(?)", array($albumID));		
	}
	
	public function getAlbum($albumID)
	{
		$r = $this->_fetchStatement("CALL sp_admin_get_album(?)", array($albumID));
		return empty($r) ? null : $r[0];
	}
	
	/**
	@param $albumCover album cover file name has form 'test.jpg'
	**/
	public function insertAlbum($albumName, $albumType, $albumCoverFileName, $albumDescription)
	{
		$returnArray = $this->_moveTempFileToMediaFolder($albumCoverFileName);
		if(!isset($returnArray['successful']) || !$returnArray['successful']) {
			return false;
		}
			
		return $this->_executeStatement("CALL sp_admin_insert_album(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", 
										array(
											self::MEDIA_URL_PREFIX . $returnArray['realFileName'],
											$returnArray['realWidth'],
											$returnArray['realHeight'],
											self::MEDIA_URL_PREFIX . $returnArray['thumbnailFileName'],											
											$returnArray['thumbnailWidth'],
											$returnArray['thumbnailHeight'],
											'Album cover',
											$albumName, 
											$albumType, 											  
											$albumDescription));
	}
	
	public function updateAlbumNoChangeCover($albumID, $albumName, $albumDescription)
	{	
		return $this->_executeStatement("CALL sp_admin_update_album_no_change_cover(?, ?, ?)", 
										array($albumID, 
											  $albumName, 
											  $albumDescription));
	}
	
	public function updateAlbum($albumID, $albumName, $albumCover, $albumDescription)
	{
		// Get album row before update it.
		$album = $this->getAlbum($albumID);
				
		// Get album cover file name
		$photoModel = new STE_Photo($this->_db);
		$photo = $photoModel->getPhoto($album['album_cover']);
		
		// Delete old album cover file.
		$successful = $this->_deleteAlbumCoverFile($this->_getFileNameFromFilePath($photo['path']),
												   $this->_getFileNameFromFilePath($photo['thumbnail_path']));		
		
		if(!$successful) {			
			return false;
		}
		
		// Move temp file to storage.
		$returnArray = $this->_moveTempFileToMediaFolder($albumCover);
		if(!isset($returnArray['successful']) && !$returnArray['successful']) {
			return false;
		}
		
		return $this->_executeStatement("CALL sp_admin_update_album(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", 
										array($albumID, 
											  self::MEDIA_URL_PREFIX . $returnArray['realFileName'],
											  $returnArray['realWidth'],
											  $returnArray['realHeight'],											  
											  self::MEDIA_URL_PREFIX . $returnArray['thumbnailFileName'],
											  $returnArray['thumbnailWidth'],
											  $returnArray['thumbnailHeight'],
											  'Album cover',
											  $albumName, 
											  $albumDescription));
	}
	
	public function deleteAlbum($albumID)
	{
		// get album row before delete it
		$album = $this->getAlbum($albumID);
		
		$successful1 = $successful2 = false;
		// get media path list to delete them all from server
		if($album['album_type'] == 'photo') {
			$photoPathList = $this->_getPhotoPathList($albumID);
			
			// Get album cover
			$photoModel = new STE_Photo($this->_db);
			$photo = $photoModel->getPhoto($album['album_cover']);
						
			// delete album cover file on server (includes "real" and "thumbnail")
			$successful1 = $this->_deleteAlbumCoverFile($this->_getFileNameFromFilePath($photo['path']), 
														$this->_getFileNameFromFilePath($photo['thumbnail_path']));
		
			// delete all media file of this album on server
			$successful2 = $this->_deletePhotoFilesByAlbum($photoPathList);
		} else {
			//$videoPathList = $this->_getVideoPathList($albumID);
		}
						
		if(!$successful1 || !$successful2) {
			return false;
		}
	
		return $this->_executeStatement("CALL sp_admin_delete_album(?)", 
										array($albumID));
	}	
	
	private function _deleteAlbumCoverFile($realFileName, $thumbnailFileName)
	{		
		$successful1 = $successful2 = false;
		if (file_exists(self::MEDIA_DIRECTORY . $realFileName)) {		
			$successful1 = unlink(self::MEDIA_DIRECTORY . $realFileName);
		}
		
		if (file_exists(self::MEDIA_DIRECTORY . $thumbnailFileName)) {		
			$successful2 = unlink(self::MEDIA_DIRECTORY . $thumbnailFileName);
		}
		
		return $successful1 & $successful2;
	}
	
	private function _deletePhotoFilesByAlbum($photoPathList)
	{			
		$successful = true;		
		foreach($photoPathList as $filePath) {		
			if (file_exists($_SERVER['DOCUMENT_ROOT'] . $filePath['path'])) {
				$successful &= unlink($_SERVER['DOCUMENT_ROOT'] . $filePath['path']);
				$successful &= unlink($_SERVER['DOCUMENT_ROOT'] . $filePath['thumbnail_path']);			
			} else {
				$successful = false;
			}
		}
		return $successful;
	}
	




}	
?>